package com.ruoyi.deviceM.controller;

import java.util.List;

import com.ruoyi.DeviceCrudConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.deviceM.domain.UserDevice;
import com.ruoyi.deviceM.service.IUserDeviceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.deviceManagement.service.DeviceCrudService;
import com.ruoyi.deviceM.domain.UserDevice;
/**
 * 设备信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Controller
@RequestMapping("/Device02/device")
public class UserDeviceController extends BaseController
{
    private String prefix = "Device02/device";

    @Autowired
    private IUserDeviceService userDeviceService;
    @Autowired
    private DeviceCrudService deviceCrudService;
    @RequiresPermissions("Device02:device:view")

    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备信息列表
     */
    @RequiresPermissions("Device02:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserDevice userDevice)
    {
        startPage();
        List<UserDevice> list = userDeviceService.selectUserDeviceList(userDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("Device02:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserDevice userDevice)
    {
        List<UserDevice> list = userDeviceService.selectUserDeviceList(userDevice);
        ExcelUtil<UserDevice> util = new ExcelUtil<UserDevice>(UserDevice.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 新增设备信息
     */
    @RequiresPermissions("Device02:device:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备信息
     */
    @RequiresPermissions("Device02:device:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserDevice userDevice)
    {
        try {
            // 构造其他参数
            JSONObject otherParams = new JSONObject();
            JSONObject result;
//String deviceName, String deviceSn, String imei, String operator, JSONObject otherParams
            // 调用 OneNet 创建设备
            deviceCrudService = new DeviceCrudConfig().deviceCrudService();
            result = deviceCrudService.createDevice(
                    userDevice.getDeviceName(),
                    userDevice.getDeviceCode(),
                    userDevice.getImei(),
                    getLoginName(), // 当前登录用户
                    otherParams
            );

            userDevice.setDeviceId(Long.valueOf(result.getString("deviceId")));
            userDevice.setProductId(String.valueOf(result.getLong("productId")));



        } catch (Exception e) {
            return AjaxResult.error("OneNet 创建设备失败：" + e.getMessage());
        }


        return toAjax(userDeviceService.insertUserDevice(userDevice));
    }

    /**
     * 修改设备信息
     */
    @RequiresPermissions("Device02:device:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        UserDevice userDevice = userDeviceService.selectUserDeviceByDeviceId(deviceId);
        mmap.put("userDevice", userDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备信息
     */
    @RequiresPermissions("Device02:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserDevice userDevice)
    {
        try {
            JSONObject otherParams = new JSONObject();
            // 调用 OneNet 创建设备
            //updateDevice(String deviceId, String deviceName, String operator, JSONObject otherParams)
            deviceCrudService.updateDevice(
                    userDevice.getDeviceId().toString(),
                    userDevice.getDeviceName(),
                    getLoginName(),
                    otherParams
            );


        } catch (Exception e) {
            return AjaxResult.error("OneNet 修改设备失败：" + e.getMessage());
        }

        return toAjax(userDeviceService.updateUserDevice(userDevice));
    }

    /**
     * 删除设备信息
     */
    @RequiresPermissions("Device02:device:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            deviceCrudService.deleteDevice(ids);

        } catch (Exception e) {
            return AjaxResult.error("OneNet 删除设备失败：" + e.getMessage());
        }


        return toAjax(userDeviceService.deleteUserDeviceByDeviceIds(ids));
    }
}
